<?php

namespace App\Http\Controllers;

use App\Imports\ProjectClassifyImport;
use App\Models\PartCompany;
use App\Models\SortLink;
use App\Models\SortLinkSearch;
use App\Services\CommonService;
use App\Services\ProjectClassifyService;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Http\JsonResponse;

class ProjectClassifyController extends Controller
{
    public $projectClassifyService;

    public function __construct(ProjectClassifyService $projectClassifyService)
    {
        parent::__construct();
        $this->projectClassifyService = $projectClassifyService;
    }

    /**
     * 导入单位工程
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/19 13:16
     */
    public function import(Request $request): JsonResponse
    {
        $this->projectClassifyService->isFileValid($request);//验证文件是否合法
        $partCompanyId = $request->input('part_company_id');

        $importCountKey = get_rand_str('import_project_classify_count');
        redis()->setex($importCountKey, 60, 0); //初始值

        Excel::import(new ProjectClassifyImport($partCompanyId, $importCountKey), $request->file('file'));

        $count = redis()->get($importCountKey);
        CommonService::addActionLog('导入项目分类', "导入【{$count}】条数据");
        redis()->del($importCountKey);

        $this->projectClassifyService->delSortLinkData($partCompanyId);

        return $this->success('', "导入【{$count}】个单位工程");
    }

    /**
     * 当前用户能看到的项目分类
     * 检索阶段用。检索按照 参见单位或项目分类，但是项目分类不同的公司或参建单位看到的不同
     * 这里通过当前登录用户的身份，展示出用户应当看到的项目分类
     * 新建参建单位分类目录
     */
    public function store(Request $request): JsonResponse
    {
        $params = $request->all();
        if ($params['id']) {
            $this->projectClassifyService->updateData($params['id'], $request->all());
            $msg = '修改成功';
        } else {
            $this->projectClassifyService->addNewData($request->all());
            $msg = '添加成功';
            $this->projectClassifyService->delSortLinkData($params['part_company_id']);
            SortLinkSearch::query()->where('project_id', $params['project_id'])->delete();
        }
        return $this->success('', $msg);
    }

    /**
     * 单位工程树
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/19 13:14
     */
    public function treeList(Request $request): JsonResponse
    {
        $partCompanyId = $request->input('part_company_id'); //参建单位 id

        $data = $this->projectClassifyService->treeClassifies($partCompanyId);

        redis()->setex("treeListData:{$partCompanyId}", 3600 * 24 * 3, json_encode($data));

        PartCompany::query()->where('id', $partCompanyId)->update(['first_unit_project_id' => $data[0]['id']]); //把当前参建单位对应的单位工程树第一个节点记下来，后续查找全部案卷用到

        return $this->success($data);
    }

    /**
     * 获取一条项目分类目录的详情
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/19 13:19
     */
    public function classifiesInfo(Request $request): JsonResponse
    {
        $id        = $request->input('id');
        $projectId = $request->input('project_id');

        $info = ProjectClassifyService::getProjectClassifiesInfo($id, $projectId);

        return $this->success($info);
    }

    /**
     * 单个软删除 （软删除相关的全部是Laravel内置操作方法）
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/19 13:19
     */
    public function destroy(Request $request): JsonResponse
    {
        $partCompanyId     = $request->input('part_company_id'); //参见单位id
        $projectClassifyId = $request->input('id'); //项目分类id
        if (!$this->projectClassifyService->delProjectClassify($partCompanyId, $projectClassifyId)) {
            return $this->error('删除失败~');
        }
        CommonService::addActionLog('删除项目分类', "数据ID:{$projectClassifyId}");

        $this->projectClassifyService->delSortLinkData($partCompanyId);

        return $this->success('', '删除成功');
    }


}
